#encoding:utf8

import copy
import json
from django.shortcuts import render_to_response
from django.http import HttpResponse

# Create your views here.
from utils.tools import get_request_field

from webserver.models import Tasker
from config import AUTH_CODE

return_data = dict(code=0, msg="", data="")


def help(request):
    # check auth
    auth_code = get_request_field(request, "auth")
    if auth_code != AUTH_CODE:
        data = copy.copy(return_data)
        data['code'] = -1
        data['msg'] = "Authorization is falied"
        return HttpResponse(json.dumps(data), content_type="application/json")

    # url patterns
    from webserver.urls import urlpatterns

    def get_url_regexs(urlpatterns):
        data = []
        for urlpattern in urlpatterns:
            regex = urlpattern.regex.pattern
            data.append(regex)
        return data

    patterns = get_url_regexs(urlpatterns)
    template_file = "help/help.html"
    model = Tasker.__doc__
    data = {
        "urlpatterns": patterns,
        "model": model
    }

    return render_to_response(template_file, data)
